#include "../zlib_libpng/png.h"
#include "../raster/image.h"

#if 0

void write_png_file(char const* file_name, flip_image_data* img)
{
	int width = flip_image_width(img), height = flip_image_height(img);
	png_byte color_type = PNG_COLOR_TYPE_RGB;
	png_byte bit_depth = 8;
	
	std::vector<png_bytep> row_pointers(height);
	for(int y = 0; y < height; ++y)
	{
		row_pointers[y] = (unsigned char*)(flip_image_pixels(img) + y*width*4);
	}

	/* create file */
	FILE *fp = fopen(file_name, "wb");
	if (!fp) throw std::runtime_error("[write_png_file] File could not be opened for writing");


	/* initialize stuff */
	png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

	if (!png_ptr)
		throw std::runtime_error("[write_png_file] png_create_write_struct failed");

	png_infop info_ptr = png_create_info_struct(png_ptr);
	if (!info_ptr)
		throw std::runtime_error("[write_png_file] png_create_info_struct failed");

	if (setjmp(png_jmpbuf(png_ptr)))
		throw std::runtime_error("[write_png_file] Error during init_io");

	png_init_io(png_ptr, fp);


	/* write header */
	if (setjmp(png_jmpbuf(png_ptr)))
		throw std::runtime_error("[write_png_file] Error during writing header");

	png_set_IHDR(png_ptr, info_ptr, width, height,
			 bit_depth, color_type, PNG_INTERLACE_NONE,
			 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);

	png_write_info(png_ptr, info_ptr);

	png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);

	/* write bytes */
	if (setjmp(png_jmpbuf(png_ptr)))
		throw std::runtime_error("[write_png_file] Error during writing bytes");

	png_write_image(png_ptr, &row_pointers[0]);


	/* end write */
	if (setjmp(png_jmpbuf(png_ptr)))
		throw std::runtime_error("[write_png_file] Error during end of write");

	png_write_end(png_ptr, NULL);
	
	png_destroy_write_struct(&png_ptr, &info_ptr);

	fclose(fp);
}

#endif